/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */
// Import("ocr.js")
// Import("utils.js")
// Import("ggUtils.js")


// SystemPrint.log(app.getAllApp())
var APP_NAME_WKLLQ = '悟空浏览器'
var PACK_NAME_WKLLQ = 'com.cat.readall'

function skip_主界面的弹窗_WKLLQ() {
    var detectsclose = opencv.findImagesEx('图色400133.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }
}

function skip_任务中心的弹窗_WKLLQ() {
    var allres = ocrAllRes()

    if (!ocrTextMoreByRes(allres, '心收下', '开心收') && ocrTextMoreByRes(allres, '恭喜你获得')) {
        //找图返回目标数组
        var detectsclose = opencv.findImagesEx('图色400133.cv');
        if (detectsclose != null) {
            agentOrHidClick(detectsclose[0])
            agentOrHidClick(ocrText('心收下', '开心收'))
        }
    }

    agentOrHidClick(ocrTextMoreByRes(allres, '心收下', '开心收'))
    agentOrHidClick(ocrTextMoreCompleteByRes(allres, '取消'))
}

function back2center_WKLLQ() {
    // if (!backUntilFindView(() => ocrText('日常任务', '金币:', '抵用金:', '点就领'))) {
    //     enter_任务中心_WKLLQ(true)
    // }
    array.forEach(element => {
        // Ddddd
    });
}


function move2顶部() {
    move2Top(7, 300, '现金收益', '赚赚卡', '活动收益')
}

// 启动任务
function start_app_WKLLQ() {
    // app.closeApp(PACK_NAME_WKLLQ)
    // sleep.millisecond(200)
    printinfoOld('start_app_WKLLQ')

    var isuccess = startApp(PACK_NAME_WKLLQ)
    if (!isuccess) {
        return false
    }

    // 跳过首页广告
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, '首页')
    }, (times) => {
        skip_主界面的弹窗_WKLLQ()

        if (times > 3) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
    }, null, 3)

    return true
}

// 进入任务中心
function enter_任务中心_WKLLQ(iskip) {
    printinfoOld('enter_任务中心_WKLLQ')

    if (iskip == undefined) iskip = false

    // 进入任务中心
    expectView(() => {
        return ocrTextMoreInRegion(0.4, 0.88, 1, 1, '金币')
    }, (times) => {
        skip_主界面的弹窗_WKLLQ()

        sleep.millisecond(500)
        SystemPrint.warn('未找到主界面期望标志位')
    }, (findView, stepTimes) => {
        if (stepTimes == 1) {
            skip_主界面的弹窗_WKLLQ()
        }
        agentOrHidClick(findView)
    }, () => {
        // 失败后的方案 或者其它方案进入
        clickOffectY(0.5, 1, 60)
    }, 3)


    if (!iskip) {
        // 期望标志位
        expectView(() => {
            return ocrText('去提现', '立即领取', '额外再领', '签到领', '看广告')
        }, (times) => {
            skip_任务中心的弹窗_WKLLQ()

            if (times > 3) {
                agentOrHidback()
                sleep.millisecond(500)
                // todo 未找到后 返回怎么做
            }
            sleep.millisecond(500)
            SystemPrint.warn('未找到任务界面期望标志位')
        }, (findView) => {
            // agentOrHidClick(findView)
        }, () => {
            clickOffectY(0.5, 1, 60)
        }, 3)
    }
}

function isInGGView_WK() {
    log('判断是否在广告页面')
    var allres = ocrAllRes()

    if (ocrTextMoreCompleteByRes(allres, '广告', '广告1)', '广告|)')) {
        return true
    }

    if (ocrTextMoreCompleteByRes(allres, '反馈', '跳过', '关闭', '广告')) {
        return true
    }

    if (ocrTextMoreByRes(allres, '继续观看', '直播间', '点击进入', '下滑', '记入奖励', '奖励时长', '后可领', '看直播开宝箱')) {
        return true
    }

    var detects播音 = opencv.findImagesEx('图色681128.cv');
    if (detects播音 != null) {
        log('检测到播音图标')
        return true
    }

    log('不在广告页面!!!')
    return false
}

function isInMainPage_WK() {
    var allres = ocrAllRes()
    if (ocrTextMoreByRes(allres, '日常任务', '领取金币', '去提现', '吃饭补贴', '福利商城')
    ) {
        log('在主页面了!')
        return true
    }

    return false
}

function sampleGGForWK(dect, skipClick, maxTimes) {
    if (dect == null) {
        return
    }
    if (maxTimes == undefined) {
        maxTimes = 12
    }

    if (!skipClick) {
        agentOrHidClick(dect)
    }

    var isInGG = false
    whilestep((a, b, canbreak) => {
        var allres = ocrAllRes()
        if (ocrTextMoreCompleteByRes(allres, '广告', '反馈', '跳过', '立即下载')
            || ocrTextMoreByRes(allres, '跳过', '反馈', '点击进入')) {
            log('在广告页面了')
            isInGG = true
            canbreak()
        }
    }, 6, 2)

    if (!isInGG && !isInGGView_WK()) {
        return
    }

    log('获取广告时长')
    var convertTime = 5
    var findtime = ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.16, 6, '秒', 's后', 's', '后')
    if (findtime) {
        convertTime = getNumber(findtime.text())
        log('广告时长' + convertTime)
        if (convertTime > 60) {
            convertTime = 30
        }
        if (convertTime < 5) {
            convertTime = 30
        }
    }

    var rgbs
    var is古籍广告 = false
    var isZbGG = false
    var isGameorDown = false
    whilestep((stepTimes, alltimes, canBreak) => {
        rgbs = get界面采样点()
        var allres = ocrAllRes()
        log('剩余执行时间: ' + (convertTime - alltimes))

        if (ocrTextMoreByRes(allres, '直播间', '点击进入', '直播中', '进入直播')) {
            isZbGG = true
            // 直播间广告
            log('直播间广告')
            agentClickRightOffectRight(ocrTextMoreByRes(allres, '取消'), 10, 1, () => {
                if (ocrWaitForMoreTextMillisecond('更多直播', '说点什么', '人气', '小时榜', 4)) {
                    agentOrHidback()
                    sleep.second(2)

                    // 返回后如果不在广告页面也不在主页面就再次返回
                    if (!isInGGView_WK() && !isInMainPage_WK()) {
                        agentOrHidback()
                        sleep.second(1)
                    }
                }


                // 如果不小心返回过了
                allres = ocrAllRes()
                if (ocrTextMoreByRes(allres, '继续', '续观看') && ocrTextMoreByRes(allres, '换一个', '直接退出', '坚持退出')) {
                    // 继续观看
                    log('继续观看')
                    agentOrHidClick(ocrText('继续', '续观看'))
                }
            })
        }
        // 判断是否是斗地主之类的
        if (ocrTextMoreByRes(allres, '斗地主', '当皇上', '种树', '快乐学习', '古籍', '芒果', '公益')
            || ocrTextMoreByRes(allres, '微信', '转微', '小游戏', '种菜')) {
            is古籍广告 = true
            log('斗地主广告')
            addExcuteFuncTimes('ct_斗地主_times')
            canBreak()
        }

        if (ocrTextMoreByRes(allres, '手游', '侠', '仙', '下载', '游戏', '梦幻', '三国', '射击')) {
            log('游戏类广告')
            isGameorDown = true
        }

        // 判断是否广告已经结束
        if (convertTime > 29 && alltimes > 30) {
            if (ocrTextMoreByRes(allres, '已领取', '再看一', '关闭', '开心收下', '领取成功', '领取奖励')) {
                canBreak()
            }
        }

    }, convertTime, 5, (canBreak) => {
        if (check界面是否不再变化(rgbs)) {
            if (ocrText('领取成功')) {
                log('界面不再变化!!!')
                canBreak()
            }
        }
    })

    if (getRandomNum(0, 100) > 50) {
        log('等待完播')
        var wbmaxTimes = 30
        var rgbs = get界面采样点()
        sleep.second(1)
        while (true) {
            if (check界面是否不再变化(rgbs) || ocrText('直播')) {
                log('完播界面不再变化!!!')
                break
            }

            wbmaxTimes = wbmaxTimes - 1
            if (wbmaxTimes < 0) {
                break
            }
            sleep.second(2)
            rgbs = get界面采样点()
        }
    }

    log('准备关闭广告')
    agentOrHidClick(ocrWaitForText('关闭', '领取成功', '跳过'))
    agentClickRight(ocrText('跳过'))


    if (isInGGView_WK()) {
        log('存在关闭识别不准确的问题')
        agentOrHidback()
        sleep.second(2)
    }

    // 判断是任务中心还是再来一次
    var allres = ocrAllRes()

    // 如果是任务中心结束迭代
    if (ocrTextMoreByRes(allres, '去提现', '金币收益')) {
        log('在主页面了2')
        return
    }

    if (maxTimes < 1) {
        log('达到看广告极限次数')
        if (!isInGGView_WK()) {
            log('在广告详情页准备返回')
            agentOrHidback()
            sleep.second(2)

            closeButton = ocrWaitForText('关闭', '领取成功', 10, 2)
            if (closeButton == null) {
                agentOrHidClick(closeButton)
                agentClickRight(ocrText('跳过'))
            } else {
                agentOrHidback()
                sleep.second(2)
            }
        }
        agentOrHidClick(ocrTextMoreByRes(allres, '退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
        return
    }


    if (isRunAppTask('taskwkllq', '单广')) {
        log('单广模式返回')
        agentOrHidClick(ocrTextMoreByRes(allres, '退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
        return
    }

    log('准备再次广告')
    var moreFlagAgain = ocrTextMoreByRes(allres, '看视频再', '再看一条')
    if (moreFlagAgain) {
        if (getDectNumber(moreFlagAgain) >= 1500) {
            sampleGGForWK(moreFlagAgain, false, maxTimes - 1)
        }
    }

    agentOrHidClick(ocrTextMoreByRes(allres, '退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
    log('结束广告')
}


function open_签到_WKLLQ() {
    log('签到')
    if (!isRunAppTask('taskwkllq', '签到')) {
        return
    }

    if (hasExcuteFunc('open_签到_WKLLQ')) {
        return
    }

    if (getExcuteFuncTimes('open_签到_WKLLQ') > 2) {
        return
    }

    if (getExcuteFuncTimes('open_签到_WKLLQ') <= 1) {
        agentOrHidClick(ocrWaitForText('额外', '外再领'), 2, () => {
            sampleGGForWK({}, true)
        })

    } else {
        skip_任务中心的弹窗_WKLLQ()
    }

    var spfalg = ocrWaitForText('看视频+')
    if (spfalg) {
        sampleGGForWK(spfalg)
    }

    addExcuteFuncTimes('open_签到_WKLLQ')
}

function open_开宝箱_WKLLQ() {
    printinfoOld('open_开宝箱_WKLLQ-------------------------------')
    skip_任务中心的弹窗_WKLLQ()

    agentOrHidClick(ocrText('开宝箱得金币'), 1, () => {
        watchGG_多次看广告(ocrTextMoreInRegion(0, 0.5, 1, 0.8, '看视频', '看广告'))
    })
}

function watch_看广告_WKLLQ() {
    printinfoOld('watch_看广告_WKLLQ-------------------------------')
    if (!isRunAppTask('taskwkllq', '看视频')) {
        return
    }

    if (hasExcuteFunc('watch_看广告_WKLLQ')) {
        return
    }
    skip_任务中心的弹窗_WKLLQ()

    move2顶部()
    // 只执行一次
    moveUp2FindWidTxtMore((findView) => {
        watchGG_多次看广告(findView)
        setExcuteFunc('watch_看广告_WKLLQ')
    }, '看广告赚金币', '看视频賺', '看视频')
}

// 进行浏览任务
function start_浏览任务_WKLLQ() {
    if (!isRunAppTask('taskwkllq', '活跃')) {
        return
    }
    if (hasExcuteFunc('start_浏览任务_WKLLQ') || getExcuteFuncTimes('start_浏览任务_WKLLQ') > 2) {
        return
    }

    log('悟空浏览任务')
    if (!start_app_WKLLQ()) {
        return
    }

    skip_主界面的弹窗_WKLLQ()

    var ocr1 = opencv.OCREx('图色157150.cv');
    if (ocr1 != null) {
        agentOrHidClick(ocr1.getAllDetect()[0])
    } else {
        click(0.2986, 0.9707)
    }

    var isInView = false
    for (let i = 0; i < 30; i++) {
        sleep.second(getRandomNum(5, 30))
        agentSwipBig()

        if (!isInView) {
            if (ocrText('写评论', '抢沙发')) {
                isInView = true
            }
        }

    }

    if (isInView) {
        setExcuteFunc('start_浏览任务_WKLLQ')
    }

    addExcuteFuncTimes('start_浏览任务_WKLLQ')
    stopAllApp()
}


function start_悟空浏览器刷单广_Task(isWhile) {
    if (!start_app_WKLLQ()) {
        return
    }
    if (isWhile == undefined) isWhile = true
    enter_任务中心_WKLLQ()

    skip_任务中心的弹窗_WKLLQ()

    logSleep('准备开始循环看悟空广告', 5)
    var is刷单广 = isRunAppTask('taskwkllq', '单广')
    var index = 0
    while (true) {
        log('等待获得惊喜奖励')
        if (ocrWaitForText('获得惊喜奖励', '恭喜你获得', '领取惊喜奖励', '额外再', '看视频+', '时奖励', '快来领取')) {
            if (is刷单广) {
                sampleGGForWK(ocrWaitForText('看视频再', '额外再', '看视频+', '看视频领', '视频领取'), false, 0)
            } else {
                sampleGGForWK(ocrWaitForText('看视频再', '额外再', '看视频+', '看视频领', '视频领取'))
            }

            skip_任务中心的弹窗_WKLLQ()
        }

        if (isRunAppTask('taskwkllq', '领取金币')) {
            if (getExcuteFuncTimes('start_悟空浏览器刷单广_Task_领取金币') < 20) {
                agentOrHidClick(ocrText('领取金币'), 2, () => {
                    addExcuteFuncTimes('start_悟空浏览器刷单广_Task_领取金币')
                    var spFlag = ocrWaitForText('看视频再', '看视频领取')
                    if (getDectNumber(spFlag) > 1200) {
                        if (is刷单广) {
                            sampleGGForWK(spFlag, false, 0)
                        } else {
                            sampleGGForWK(spFlag)
                        }
                    }

                    skip_任务中心的弹窗_WKLLQ()
                })
            }
        }



        if (is刷单广) {
            log('等待开宝箱得')
            if (isRunAppTask('taskwkllq', '开宝箱')) {
                agentOrHidClick(ocrText('开宝箱得'), 2, () => {
                    var spFlag = ocrWaitForText('看视频额外', '看视频再')
                    if (getDectNumber(spFlag) > 1200) {
                        sampleGGForWK(ocrWaitForText('看视频额外', '看视频再'), false, 0)
                    }
                    skip_任务中心的弹窗_WKLLQ()
                })
            }
        }

        if (isRunAppTask('taskwkllq', '气泡')) {
            log('等待看视频气泡')
            agentOrHidClick(ocrTextInRegion(0, 0, 1, 0.3, '看视频'), 2, () => {
                var spFlag = ocrWaitForText('看视频再', '看视频领', '视频领取')
                if (getDectNumber(spFlag) > 1200) {
                    if (is刷单广) {
                        sampleGGForWK(spFlag, false, 0)
                    } else {
                        sampleGGForWK(spFlag)
                    }
                }

                skip_任务中心的弹窗_WKLLQ()
            })
        }

        if (index % 5 == 0) {
            if (isInGGView_WK()) {
                agentOrHidClick(ocrText('关闭', '领取成功', '跳过'))
                agentClickRight(ocrText('跳过'))


                if (isInGGView_WK()) {
                    log('存在关闭识别不准确的问题')
                    agentOrHidback()
                    sleep.second(2)
                }

                agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
            }
        }

        if (!isWhile) {
            break
        } else {
            skip_任务中心的弹窗_WKLLQ()

            sleep.second(3)
        }
        index = index + 1
        addExcuteFuncTimes('start_悟空浏览器刷单广_Task')

    }
}

function start_悟空浏览器刷多广_Task() {
    if (!start_app_WKLLQ()) {
        return
    }

    enter_任务中心_WKLLQ()

    skip_任务中心的弹窗_WKLLQ()

    logSleep('准备开始循环看悟空广告', 5)

    open_签到_WKLLQ()

    while (true) {
        log('获得惊喜奖励')
        if (ocrWaitForText('获得惊喜奖励', '恭喜你获得', '领取惊喜奖励')) {
            sampleGGForWK(ocrText('看视频再'))
            skip_任务中心的弹窗_WKLLQ()
        }

        log('领取金币')
        agentOrHidClick(ocrText('领取金币'), 2, () => {
            sampleGGForWK(ocrWaitForText('看视频再'))
            skip_任务中心的弹窗_WKLLQ()
        })


        log('看视频气泡')
        agentOrHidClick(ocrTextInRegion(0, 0, 1, 0.3, '看视频'), 2, () => {
            sampleGGForWK(ocrWaitForText('看视频领取'))
            skip_任务中心的弹窗_WKLLQ()
        })

    }
}

function start_收集金币_WKLLQ() {
    if (!HANGUP_APPNAMES.find(item => item == '悟空浏览器')) {
        log('悟空浏览器不刷金')
        return
    }
    printinfoOld('start_收集金币_WKLLQ')
    currentAppPackName = PACK_NAME_WKLLQ
    // closeApp(PACK_NAME_WKLLQ)

    if (!start_app_WKLLQ()) {
        return
    }

    addExcuteFuncTimes('start_收集金币_WKLLQ')

    enter_任务中心_WKLLQ()

    open_签到_WKLLQ()

    watch_看广告_WKLLQ()

    open_开宝箱_WKLLQ()

    stopAllApp()
    // closeApp(PACK_NAME_WKLLQ)
}